/*
 * FeedParser.fx
 *
 * Created on 14-apr-2009, 10:59:51
 */

package fotoboek.parser;
import java.io.InputStream;
import javafx.data.pull.Event;
import javafx.data.pull.PullParser;
import javafx.data.xml.QName;
import fotoboek.model.model.Album;
import fotoboek.model.model.Photo;

/**
 * @author r.mohammadi
 */

public class FeedParser {
    
    // Error Message (if any)
    public var errorMessage = "";
    
    // Information about all photos and albums
    var photos: Photo[];
    var albums:Album[];
    
    // Completion callback that also delivers parsed photo metadata
    public var onDonePhoto: function(data : Photo[]) = null;
    public var onDoneAlbum: function(data : Album[]) = null;

    public function parseAlbums(input: InputStream) {
        // Parse the input data (Album Metadata) and construct Album instances
        def parser = PullParser {                     
            input: input 
            documentType: PullParser.XML
            ignoreWhiteSpace:true
            onEvent: function(event: Event) {                                    
                    def album = Album {};
                    var i:Integer;
                    if (event.type == PullParser.START_ELEMENT) {
                        //println("### START ELEMENT ###");
                        if(event.qname.name == "album" and event.level == 1){
                            album.id = i;//event.getAttributeValue(QName{name:"id"}).trim() as String;
                            album.name = event.getAttributeValue(QName{name:"name"}).trim() as String;
                            album.items = event.getAttributeValue(QName{name:"items"}).trim() as String;
                            album.caption = event.getAttributeValue(QName{name:"caption"}).trim() as String;
                            album.url = event.getAttributeValue(QName{name:"url"}).trim() as String;
                            album.photographer = event.getAttributeValue(QName{name:"fotograaf"}).trim() as String;
                           album.cover = event.getAttributeValue(QName{name:"cover"}).trim() as String;
//                            println("###    ALBUM {album.id} ADDED  {i++}   ###");
//                            println("Name:{album.name}");
//                            println("Items:{album.items}");
//                            println("Caption:{album.caption}");
//                            println("Url:{album.url}");
//                            println("Photographer:{album.photographer}");
//                            println("Cover:{album.cover}");
//                            println("###############################");
//                            println(" ");
                            
                          insert album into albums;
                          i += 1;
                        }
                     }
                      else if (event.type == PullParser.END_DOCUMENT) {
                        //println("### END OF DOCUMENT ###");
                        onDoneAlbum(albums);
                    }
                else if(event.qname.name == "err" and event.level == 1) {
                    errorMessage = event.getAttributeValue(QName{name:"msg"});
                }
             }//onEvent
           }//parser
        parser.parse();
    }
    public function parsePhotos(input: InputStream) {
        // Parse the input data (Photo Metadata or Album Metadata) and construct Photo or Album instances
        def parser = PullParser {
            input: input
            documentType: PullParser.XML            
            var i:Integer
            var title:String;
            var url:String;
            var original:String;
            var origHeight:String;
            var origWidth:String;
            var thumbnail:String;
            var thumbHeight:String;
            var thumbWidth:String;

            onEvent: function(event: Event) {                               
                if (event.type == PullParser.START_ELEMENT and event.level == 0) {
                    //println("### START ELEMENT ###");
                    url = event.getAttributeValue(QName{name:"url"}).trim() as String;
                     println("photoURL:{url}");
                     }
                    if (event.type == PullParser.END_ELEMENT and event.level == 3) {
                          if (event.qname.name == "itemCaption") {
                            title = event.text.trim();
                            //println("Title {photo.title}");
                          }
                              else if (event.qname.name == "itemLargeImage") {
                                original = event.text.trim();
                               //  println("photoOriginal:{original}");
                                }
                                    else if (event.qname.name == "itemHeight") {
                                        origHeight = event.text.trim() as String;
                                       // println("photoOrigHeight:{origHeight}");
                                    }
                                        else if (event.qname.name == "itemWidth") {
                                            origWidth = event.text.trim() as String;
                                          //println("photoOrigWidth:{origWidth}");
                                         }
                                            else if (event.qname.name == "itemThumbnailImage") {
                                                thumbnail = event.text.trim();
                                             // println("photoTumbnail:{event.text.trim()}");
                                             }
                                                else if (event.qname.name == "itemThumbnailHeight") {
                                                    thumbHeight = event.text.trim() as String;
                                                   // println("photoThumbHeight:{photo.thumbHeight}");
                                                }
                                                     else if (event.qname.name == "itemThumbnailWidth") {
                                                        thumbWidth = event.text.trim() as String;
                                                       // println("photoThumbWidth:{photo.thumbWidth}");                                                       
                                                     }                                                          
                         } 
                            else if (event.type == PullParser.END_ELEMENT and event.level == 2){
                              def photo = Photo{
                                  id:i
                                  origHeight:origHeight
                                  origWidth:origWidth
                                  original:original
                                  thumbHeight:thumbHeight
                                  thumbWidth:thumbWidth
                                  thumbnail:thumbnail
                                  title:title
                                  url:url
                                }
                                
                                if(not thumbnail.equals("")){
                                    //println("String = niet leeg");
                                
                                  insert photo into photos;
//                                  println("###  PHOTO {i} ADDED     ###");
//                                  println("Title:       {photo.title} | {title}");
//                                  println("Original:    {photo.original} | {original}");
//                                  println("O Height:    {photo.origHeight} | {origHeight}");
//                                  println("O Width:     {photo.origWidth} | {origWidth}");
//                                  println("Thumbnail:   {photo.thumbnail} | {thumbnail}");
//                                  println("T Height:    {photo.thumbHeight} | {thumbHeight}");
//                                  println("T Width:     {photo.thumbWidth} | {thumbWidth}");
//                                  println("###############################");
//                                  println(" ");
                                  i++;
                                }
                                }
                               if (event.type == PullParser.END_DOCUMENT) {
                                   //  println("### END OF DOCUMENT ###");
                                     onDonePhoto(photos);
                                   }
                                    else if(event.qname.name == "err" and event.level == 1) {
                                        errorMessage = event.getAttributeValue(QName{name:"msg"});
                                            }
         }//onEvent
        }//parser
        parser.parse();
    }//parsePhotos
}
